package com.yhd.admin.web.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yhd.admin.bean.Function;
import com.yhd.admin.bean.Role;
import com.yhd.admin.bean.RoleFuncPriv;
import com.yhd.admin.bean.RoleMenuPriv;
import com.yhd.admin.bean.SysMenu;
import com.yhd.admin.service.FunctionService;
import com.yhd.admin.service.RoleFuncPrivService;
import com.yhd.admin.service.RoleMenuPrivService;
import com.yhd.admin.service.RoleService;
import com.yhd.admin.service.SysMenuService;
import com.yhd.admin.utils.EasyuiTreeNode;
import com.yhd.admin.utils.ServiceMessage;

@Controller
@RequestMapping("/priv/role")
public class PrivRoleController {
	@Autowired
	private RoleService roleService;
	@Autowired
	private SysMenuService sysMenuService;
	@Autowired
	private FunctionService functionService;
	@Autowired
	private RoleMenuPrivService rmps;
	@Autowired
	private RoleFuncPrivService rfps;
	
	@RequestMapping()
	public String showPage() {
		return "priv-role";
	}

	// 获取角色数据
	@RequestMapping("/list")
	public @ResponseBody Object list(Role role, int page, int rows) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("total", roleService.getCount(role));
		List<Role> roles = roleService.getRoles(role, page, rows);
		map.put("rows", roles);
		return map;
	}
	
	//同步加载菜单权限
	@RequestMapping("/menu/tree")
	public @ResponseBody Object getMenuByPid(@RequestParam(defaultValue = "0") int id, int roleId) {
		List<SysMenu> menus = sysMenuService.getSyncChildrenByPid(id);

		List<EasyuiTreeNode> nodes = new ArrayList<EasyuiTreeNode>();
		// 对象转换
		for (SysMenu m : menus) {
			EasyuiTreeNode node = ToEasyuiTreeNode(m);
			nodes.add(node);
		}
		
		//进行权限判断
		List<RoleMenuPriv> mPrivs = rmps.getRoleMenuPrivByRoleId(roleId);
		for (RoleMenuPriv rmp : mPrivs) {
			int menuId = rmp.getMenu_id();
			for (EasyuiTreeNode node : nodes) {
				EasyuiTreeNode find = findEasyuiTreeNodeById(menuId,node);
				if (find!=null) {
					find.setChecked(true);
					break;
				}
			}
		}
		return nodes;
	}
	//添加菜单权限
	@RequestMapping("/menu/add")
	public @ResponseBody Object addMenu(int roleId, String ids) {
		rmps.addRoleMenuPriv(roleId, ids);
		return ServiceMessage.ok();
	}
	//添加功能权限
	@RequestMapping("/func/add")
	public @ResponseBody Object addFunc(int roleId, String ids) {
		rfps.addRoleFuncPriv(roleId, ids);
		return ServiceMessage.ok();
	}
	
	//同步加载功能数据
	@RequestMapping(value = "/func/tree", method = RequestMethod.GET)
	public @ResponseBody Object getList(@RequestParam(defaultValue = "0") int id,int roleId) {
		List<Function> sons = functionService.getSyncChildrenByPid(id);
		List<EasyuiTreeNode> nodes = new ArrayList<EasyuiTreeNode>();
		for (Function s : sons) {
			EasyuiTreeNode node = ToEasyuiTreeNode(s);
			nodes.add(node);
		}
		//查询权限
		List<RoleFuncPriv> fPrivs = rfps.getRoleFuncPrivByRoleId(roleId);
		for (RoleFuncPriv rfp : fPrivs) {
			int FuncId = rfp.getFunc_id();
			for (EasyuiTreeNode node : nodes) {
				EasyuiTreeNode find = findEasyuiTreeNodeById(FuncId,node);
				if (find!=null) {
					find.setChecked(true);
					break;
				}
			}
		}
		return nodes;
	}
	
	//递归转换同步菜单节点数据
	private EasyuiTreeNode ToEasyuiTreeNode(SysMenu menu) {
		EasyuiTreeNode node = new EasyuiTreeNode();
		node.setId(menu.getId());
		node.setText(menu.getName());
		
		// 判断是否叶子节点
		if (menu.getUrl() != null) {
			node.getAttributes().put("url", menu.getUrl());
			node.setState("open");
		}else{
			List<EasyuiTreeNode> nodes = node.getChildren();
			//添加children
			List<SysMenu> children = menu.getChildren();
			for(SysMenu child: children){
				EasyuiTreeNode n = ToEasyuiTreeNode(child);
				nodes.add(n);
			}
		}
		return node;
	}

	// 同步转换功能节点(递归转换)
	private EasyuiTreeNode ToEasyuiTreeNode(Function f) {
		EasyuiTreeNode node = new EasyuiTreeNode();
		node.setId(f.getId());
		node.setText(f.getName());
		// 判断是否叶子节点
		if (f.getUrl() != null) {
			node.getAttributes().put("url", f.getUrl());
			node.setState("open");
		}else{
			List<Function> children = f.getChildren();
			List<EasyuiTreeNode> nodes = node.getChildren();
			for(Function child : children){
				EasyuiTreeNode son = ToEasyuiTreeNode(child);
				nodes.add(son);
			}
		}
		return node;
	}
		
	
	//递归查找指定的节点
	private EasyuiTreeNode findEasyuiTreeNodeById(int id,EasyuiTreeNode node){
		EasyuiTreeNode find = null;
		if (node.getId().intValue() == id) {
			find=node;
		}else{
			//如果是父节点
			if(node.getAttributes().get("url")==null){
				List<EasyuiTreeNode> children = node.getChildren();
				for(EasyuiTreeNode child : children){
					find = findEasyuiTreeNodeById(id,child);
					//找到就跳出当前循环
					if(find!=null){
						break;
					}
				}
			}
		}
		return find;
	}
}
